from boot import QLabel, Qt, Signal, QWidget, Union, ToolTipFilter, QPixmap, QSizePolicy


__all__ = ["QQ9SquareButton", "QQ9PushButton", "QQ9PrimaryPushButton", "QQ9RoundPushButton"]


class QQ9SquareButton(QLabel):

    sgn_clicked = Signal()

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    icon_path: str = "",
                    tips: str = ""
                 ):
        super().__init__(parent=parent)

        # 基础设置
        self.setFixedSize(30, 30)
        self.setContentsMargins(0, 0, 0, 0)
        self.setAlignment(Qt.AlignCenter)

        # 悬浮提示
        self.installEventFilter(ToolTipFilter(self, 100))
        self.setToolTip(self.tr(tips))

        # 焦点策略
        self.setFocusPolicy(Qt.ClickFocus)

        # 设置图标
        self.setPixmap(QPixmap(icon_path))

        # 设置样式
        self._set_style_sheet()

    def _set_style_sheet(self):
        self.setStyleSheet(
            """
            QQ9SquareButton {
                background-color: #F5F6F7;
                border: none;
                border-radius: 4px;
            }
            QQ9SquareButton:hover {
                background-color: #EAEEF3;
            }
            QQ9SquareButton:pressed {
                background-color: #E7EAEF;
            }
            """
        )

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.sgn_clicked.emit()


class _QQ9PushButtonBase(QLabel):

    sgn_clicked = Signal()

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    text: str = "",
                 ):
        super().__init__(parent=parent)

        # 基础设置
        self.setContentsMargins(0, 0, 0, 0)
        self.setAlignment(Qt.AlignCenter)

        # 设置文本
        self.setText(text)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.sgn_clicked.emit()


class QQ9PushButton(_QQ9PushButtonBase):

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    text: str = "",
                 ):
        super().__init__(parent=parent, text=text)

        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)

        self.setStyleSheet(
            """
            QQ9PushButton {
                background-color: transparent;
                border: 1px solid #C0C1C3;
                border-radius: 4px;
                font-family: "微软雅黑";
                font-size: 14px;
                color: #000000;
            }
            QQ9PushButton:hover {
                background-color: #E0E0E0;
            }
            """
        )


class QQ9PrimaryPushButton(_QQ9PushButtonBase):

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    text: str = "",
                 ):
        super().__init__(parent=parent, text=text)

        self.setStyleSheet(
            """
            QQ9PrimaryPushButton {
                background-color: #0099FF;
                border: 1px solid #0099FF;
                border-radius: 4px;
                font-family: "微软雅黑";
                font-size: 14px;
                color: #FFFFFF;
            }
            QQ9PrimaryPushButton:hover {
                background-color: #008DEB;
            }
            """
        )


class QQ9RoundPushButton(QLabel):

    sgn_clicked = Signal()

    def __init__(self,
                    parent: Union[QWidget, None] = None,
                    icon_path: str = "",
                    radius: int = 15
                 ):
        super().__init__(parent=parent)

        self.radius = radius

        # 基础设置
        self.setFixedSize(radius * 2, radius * 2)
        self.setContentsMargins(0, 0, 0, 1)
        self.setAlignment(Qt.AlignCenter)

        # 焦点策略
        self.setFocusPolicy(Qt.ClickFocus)

        # 设置图标
        self.setPixmap(QPixmap(icon_path))

        # 设置样式
        self._set_style_sheet()

    def _set_style_sheet(self):
        self.setStyleSheet(
            """
            QQ9RoundPushButton {
                background-color: #E5E6E7;
                border: none;
                border-radius: $px;
            }
            QQ9RoundPushButton:hover {
                background-color: #D5D6D7;
            }
            QQ9RoundPushButton:pressed {
                background-color: #D5D6D7;
            }
            """.replace("$", str(self.radius))
        )

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.sgn_clicked.emit()

